import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test_project/module/inherited_test_page/share_date_widget.dart';

class InheritedTestSubWidget extends StatefulWidget {
  @override
  State createState() => new _InheritedTestSubWidgetState();
}

class _InheritedTestSubWidgetState extends State<InheritedTestSubWidget> {
  var _currCount = 0;

  @override
  Widget build(BuildContext context) {
    //使用InheritedWidget中的共享数据
    final dataString = ShareDataWidget.of(context).data.toString();
    print("sub widget did rebuild");
    return Column(
      children: <Widget>[
        Container(
          child: Text(
            "$dataString",
            style: TextStyle(
              color: Colors.purple,
              fontSize: 30,
              fontWeight: FontWeight.w600,
            ),
          ),
        ),
        FlatButton(
          padding: EdgeInsets.symmetric(vertical: 20),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: <Widget>[
              Text(
                "当前点击次数：${this._currCount.toString()}",
                style: TextStyle(
                  color: Colors.blue,
                  fontSize: 30,
                  fontWeight: FontWeight.w600,
                ),
              )
            ],
          ),
          onPressed: () {
            setState(() {
              _currCount++;
            });
          },
        )
      ],
    );
  }

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    //父或祖先widget中的InheritedWidget改变(updateShouldNotify返回true)时会被调用。
    //如果build中没有依赖InheritedWidget，则此回调不会被调用。
    print("Dependencies change");
  }
}
